<script setup>
import {reactive} from "vue";
import {Search} from "@element-plus/icons-vue";
import request from "@/utils/request";
import {ElMessage, ElMessageBox} from "element-plus";

/**
 * 本地存储的账户信息
 *
 * @type {any}
 */
const account = JSON.parse(localStorage.getItem('account-info') || '{}')

/**
 * 是否是修改的判断
 *
 * @type {boolean}
 */
let isEdit = false

/**
 * 专业值
 *
 * @type {Ref<UnwrapRef<string>>}
 */
let majorOptions = []

/**
 * 企业值
 *
 * @type {Ref<UnwrapRef<string>>}
 */
let businessOptions = []

/**
 * 订单班列表数据
 *
 * @type {UnwrapNestedRefs<{total: number, form: {orderClassNo: string, orderClassAdministratorName: string, orderClassName: string, orderClassBusinessRoomNo: string, orderClassIntroduction: string, orderClassAdministratorNo: string, orderClassBusinessRoomName: string, orderClassCreateTime: string, orderClassStartTime: string, orderClassBusinessOrderClassHeadName: string, orderClassUpdateMan: string, orderClassEndTime: string, orderClassBusinessNo: string, orderClassCreateMan: string, orderClassState: number, orderClassStateName: string, orderClassBusinessName: string, orderClassMajorName: string, orderClassUpdateTime: string, orderClassRealityNumber: number, orderClassMajorNo: string}, formVisible: boolean, orderClassName: string, pageSize: number, tableData: *[], pageNum: number}>}
 */
const orderClassData = reactive({
    orderClassName: '',
    tableData: [],
    total: 0,
    pageSize: 5,
    pageNum: 1,
    formVisible: false, // 是否显示弹窗
    form: {
        orderClassNo: '',
        orderClassName: '',
        orderClassIntroduction: '',
        orderClassState: 0,
        orderClassStateName: '',
        orderClassRealityNumber: 0,
        orderClassStartTime: '',
        orderClassEndTime: '',
        orderClassAdministratorNo: '',
        orderClassAdministratorName: '',
        orderClassBusinessNo: '',
        orderClassBusinessName: '',
        orderClassBusinessOrderClassHeadName: '',
        orderClassBusinessRoomNo: '',
        orderClassBusinessRoomName: '',
        orderClassMajorNo: '',
        orderClassMajorName: '',
        orderClassCreateMan: '',
        orderClassCreateTime: '',
        orderClassUpdateMan: '',
        orderClassUpdateTime: '',
    }
})

/**
 * 加载订单班
 */
const loadOrderClass = () => {
    request.get(`/api/order-class/find-order-class-page`, {
        params: {
            pageNum: orderClassData.pageNum,
            pageSize: orderClassData.pageSize,
            orderClassAdminNo: account.accountNo,
            orderClassName: orderClassData.orderClassName,
        }
    }).then(res => {
        orderClassData.tableData = res.data?.list || []
        orderClassData.total = res.data?.list.map(item => item.orderClassCountNumber)[0] || 0;
    })
}
// 调用订单班加载方法，获取后台数据
loadOrderClass()

/**
 * 翻页
 */
const handelCurrentChange = () => {
    // 翻页时，重新加载数据即可
    loadOrderClass()
}

/**
 * 重置
 */
const reset = () => {
    orderClassData.orderClassName = ''
    // 重置时，重新加载数据即可
    loadOrderClass()
}

/**
 * 新增订单班
 */
const addOrderClass = () => {
    isEdit = false
    // 清空上次残留的数据
    orderClassData.form = {}
    orderClassData.formVisible = true
}

/**
 * 加载专业
 */
const loadMajor = () => {
    request.get(`/api/major/find-major-order-class-admin-no`, {
        params: {
            orderClassAdminNo: account.accountNo
        }
    }).then(res => {
        majorOptions = res.data
    })
}
// 调用专业加载方法，获取后台数据
loadMajor()

/**
 * 加载企业
 */
const loadBusiness = () => {
    request.get(`/api/business/find-business`, {}).then(res => {
        businessOptions = res.data
    })
}
// 调用专业加载方法，获取后台数据
loadBusiness()

/**
 * 提交新增的订单班
 */
const submitAddOrderClass = () => {
    if (account.accountRoleName === 'visitor-order-class-admin') {
        ElMessage.info("游客-订单班管理员仅可以查看信息")
    } else {
        orderClassData.form.orderClassAdministratorNo = account.accountNo
        orderClassData.form.orderClassCreateMan = account.accountNo
        request.post(`/api/order-class/add-order-class`, orderClassData.form).then(res => {
            if (res.code === "200") {
                // 重新获取数据
                loadOrderClass()
                // 关闭弹窗
                orderClassData.formVisible = false
                ElMessage.success("新增成功")
            } else {
                ElMessage.error(res.msg)
            }
        })
    }
}

/**
 * 修改订单班
 *
 * @param row 按行修改
 */
const editOrderClass = (row) => {
    isEdit = true
    orderClassData.form = JSON.parse(JSON.stringify(row))
    orderClassData.formVisible = true
}

/**
 * 提交修改的订单班
 */
const submitEditOrderClass = () => {
    if (account.accountRoleName === 'visitor-order-class-admin') {
        ElMessage.info("游客-订单班管理员仅可以查看信息")
    } else {
        orderClassData.form.orderClassAdministratorNo = account.accountNo
        orderClassData.form.orderClassCreateMan = account.accountNo
        request.put(`/api/order-class/update-order-class`, orderClassData.form).then(res => {
            if (res.code === "200") {
                // 重新获取数据
                loadOrderClass()
                // 关闭弹窗
                orderClassData.formVisible = false
                ElMessage.success("编辑成功")
            } else {
                ElMessage.error(res.msg)
            }
        })
    }
}

/**
 * 删除订单班
 *
 * @param orderClassNo 班号
 */
const deleteOrderClass = (orderClassNo) => {
    ElMessageBox.confirm("删除数据后无法恢复，您确认删除此条数据？", "删除确认", {type: 'warning'}).then(res => {
        if (account.accountRoleName === 'visitor-order-class-admin') {
            ElMessage.info("游客-订单班管理员仅可以查看信息")
        } else {
            request.delete(`/api/order-class/delete-order-class`, {
                params: {
                    orderClassNo: orderClassNo
                }
            }).then(res => {
                if (res.code === "200") {
                    // 重新获取数据
                    loadOrderClass()
                    ElMessage.success("删除成功")
                } else {
                    ElMessage.error(res.msg)
                }
            })
        }
    }).catch(() => {
        ElMessage({
            type: "info",
            message: "取消删除",
        })
    })
}

</script>

<template>
    <div v-if="account.accountRoleName === 'common-order-class-admin' || account.accountRoleName === 'visitor-order-class-admin'">
        <div class="card" style="margin-bottom: 10px;">
            <el-input style="width: 300px; margin-right: 10px;" v-model="orderClassData.orderClassName"
                      placeholder="请输入订单班名称查询"
                      :prefix-icon="Search"/>
            <el-button @click="loadOrderClass" type="primary" style="margin-left: 10px;">查询</el-button>
            <el-button @click="reset" type="info">重置</el-button>
        </div>
        <div class="card" style="margin-bottom: 10px;">
            <div>
                <el-button @click="addOrderClass" type="primary" style="margin-bottom: 10px;">新增</el-button>
            </div>
            <div>
                <el-table :data="orderClassData.tableData" style="width: 100%">
                    <el-table-column prop="orderClassNo" label="班号"/>
                    <el-table-column prop="orderClassName" label="班名" width="100px"/>
                    <el-table-column prop="orderClassRealityNumber" label="人数" width="80px"/>
                    <el-table-column prop="orderClassMajorName" label="专业"/>
                    <el-table-column prop="orderClassBusinessName" label="企业"/>
                    <el-table-column prop="orderClassBusinessOrderClassHeadName" label="企班负责人" width="100px"/>
                    <el-table-column prop="orderClassBusinessRoomName" label="办公室"/>
                    <el-table-column prop="orderClassStateName" label="状态" width="100px"/>
                    <el-table-column label="操作">
                        <template #default="scope">
                            <el-button @click="editOrderClass(scope.row)" type="primary" size="small" plain>编辑
                            </el-button>
                            <el-button @click="deleteOrderClass(scope.row.orderClassNo)" type="danger" size="small"
                                       plain>删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <div class="card">
            <el-pagination v-model:current-page="orderClassData.pageNum"
                           v-model:page-size="orderClassData.pageSize"
                           @current-change="handelCurrentChange"
                           background layout="prev, pager, next" :total="orderClassData.total"/>
        </div>

        <el-dialog v-model="orderClassData.formVisible" :close-on-click-modal="false" title="订单班信息">
            <el-form :model="orderClassData.form" label-width="100px" label-position="right"
                     style="padding-right: 30px;" v-if="isEdit === false">
                <el-form-item label="班号">
                    <el-input placeholder="请输入班号" v-model="orderClassData.form.orderClassNo"
                              autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="班名">
                    <el-input placeholder="请输入班名" v-model="orderClassData.form.orderClassName" autocomplete="off"
                              :maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="班级介绍">
                    <el-input
                            v-model="orderClassData.form.orderClassIntroduction"
                            :autosize="{ minRows: 4, maxRows: 8 }"
                            type="textarea"
                            placeholder="字数不得超过300字"
                            :maxlength="300"
                    />
                </el-form-item>
                <div class="block" style="margin-bottom: 20px; margin-left: 45px">
                    <el-select
                            v-model="orderClassData.form.orderClassMajorNo"
                            class="m-2"
                            placeholder="请选择专业"
                            style="width: 525px"
                    >
                        <el-option
                                v-for="item in majorOptions"
                                :key="item.majorNo"
                                :label="item.majorName"
                                :value="item.majorNo"
                        />
                    </el-select>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <el-select
                            v-model="orderClassData.form.orderClassBusinessNo"
                            class="m-2"
                            placeholder="请选择企业名"
                            style="width: 525px"
                    >
                        <el-option
                                v-for="item in businessOptions"
                                :key="item.businessNo"
                                :label="item.businessName"
                                :value="item.businessNo"
                        />
                    </el-select>
                </div>
                <div class="block" style="margin-bottom: 20px;margin-left: 45px">
                    <el-date-picker
                            v-model="orderClassData.form.orderClassStartTime"
                            type="date"
                            placeholder="请选择开班时间"
                            style="width: 525px"
                    />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <el-date-picker
                            v-model="orderClassData.form.orderClassEndTime"
                            type="date"
                            placeholder="请选择结班时间"
                            style="width: 525px"
                    />
                </div>
            </el-form>
            <el-form :model="orderClassData.form" label-width="100px" label-position="right"
                     style="padding-right: 30px;" v-if="isEdit === true">
                <el-form-item label="班号">
                    <el-input placeholder="请输入班号" v-model="orderClassData.form.orderClassNo"
                              autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="班名">
                    <el-input placeholder="请输入班名" v-model="orderClassData.form.orderClassName" autocomplete="off"
                              :maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="班级介绍">
                    <el-input
                            v-model="orderClassData.form.orderClassIntroduction"
                            :autosize="{ minRows: 4, maxRows: 8 }"
                            type="textarea"
                            placeholder="字数不得超过300字"
                            :maxlength="300"
                    />
                </el-form-item>
                <div class="block" style="margin-bottom: 20px;margin-left: 45px">
                    <el-select
                            v-model="orderClassData.form.orderClassMajorNo"
                            class="m-2"
                            placeholder="请选择专业"
                            style="width: 525px"
                    >
                        <el-option
                                v-for="item in majorOptions"
                                :key="item.majorNo"
                                :label="item.majorName"
                                :value="item.majorNo"
                        />
                    </el-select>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <el-select
                            v-model="orderClassData.form.orderClassBusinessNo"
                            class="m-2"
                            placeholder="请选择企业名"
                            style="width: 525px"
                    >
                        <el-option
                                v-for="item in businessOptions"
                                :key="item.businessNo"
                                :label="item.businessName"
                                :value="item.businessNo"
                        />
                    </el-select>
                </div>
                <div class="block" style="margin-bottom: 20px;margin-left: 45px">
                    <el-date-picker
                            v-model="orderClassData.form.orderClassStartTime"
                            type="date"
                            placeholder="请选择开班时间"
                            style="width: 525px"
                    />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <el-date-picker
                            v-model="orderClassData.form.orderClassEndTime"
                            type="date"
                            placeholder="请选择结班时间"
                            style="width: 525px"
                    />
                </div>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="orderClassData.formVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitAddOrderClass" v-if="isEdit === false">提交</el-button>
                    <el-button type="primary" @click="submitEditOrderClass" v-if="isEdit === true">保存编辑</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<style scoped>

</style>